var options;

var charts = (function() {

	var defaults = {
		reprData : null,
        emplData : null,
        offData : null
	}


    var defaultIndexToShowPieChart;
	var setOptions = function(o) {
		options = $.extend({}, defaults, o);
	}

	var render = function() {
		YUI().use([ 'charts', 'charts-legend' ], function(Y) {

			var emplGrowingTooltip = {
				styles : {
					backgroundColor : "#333",
					color : "#eee",
					borderColor : "#fff",
					textAlign : "center"
				},
				markerLabelFunction : function(categoryItem, valueItem, itemIndex, series, seriesIndex) {
					var msg = document.createElement("span");
					msg.appendChild(document.createTextNode(valueItem.value + " employees in " + options.emplData.tooltips[itemIndex]));
					return msg;
				}
			};

			// Chart that shows growing of employees number.
            var absEmplGrowingChart = new Y.Chart({
                dataProvider : options.emplData.chartData,
                tooltip : emplGrowingTooltip,
                horizontalGridlines : true,
                render : "#empl_growing_chart"
            });

            var offGrowingTooltip = {
                styles : {
                    backgroundColor : "#333",
                    color : "#eee",
                    borderColor : "#fff",
                    textAlign : "center"
                },
                markerLabelFunction : function(categoryItem, valueItem, itemIndex, series, seriesIndex) {
                    var msg = document.createElement("span");
                    msg.appendChild(document.createTextNode(valueItem.value + " offices in " + categoryItem.value));
                    return msg;
                }
            };

			// Chart that shows growing of offices number.
			var absOfficeGrowingChart = new Y.Chart({
				axes : {
					category : {
						keys : [ "category" ],
						type : "category",
						styles : {
							label : {
								rotation : -45
							}
						}
					}
				},
				horizontalGridlines : true,
				tooltip : offGrowingTooltip,
				dataProvider : options.offData,
				render : "#offices_growing_chart"
			});

            var reprTooltip = {
                styles : {
                    backgroundColor : "#333",
                    color : "#eee",
                    borderColor : "#fff",
                    textAlign : "center"
                },
                markerLabelFunction : function(categoryItem, valueItem, itemIndex, series, seriesIndex) {
                    var total = series.getTotalValues(),
                       pct = Math.round((valueItem.value / total) * 10000)/100,
                       msg = document.createElement("span");
                    msg.appendChild(document.createTextNode(valueItem.value + " employees in " + categoryItem.value));
                    msg.appendChild(document.createElement("br"));
                    msg.appendChild(document.createTextNode(pct + "%"));
                    return msg;
                }
            };

			var representationChart= new Y.Chart({
                categoryKey : "category",
                seriesKeys : [ "value" ],
                dataProvider : options.reprData,
                type : "pie",

                legend : {
                    styles : {
                        hAlign : "center",
                        hSpacing : 4
                    }
                },
                seriesCollection : [ {
                    categoryKey : "category",
                    valueKey : "value"
                } ],
                tooltip : reprTooltip,
                render : "#representation_chart"
            });

		});
	}

	return {
		render : render,
		setOptions : setOptions
	}
})()